var comm = require("comm")
cc.Class({
    extends: comm,

    properties: {
        lv:1
    },

    setInfo(target,lv){
        this.lv = lv;
        this.targetPos = this.deepClone(target.position);
    },

    lookAtObj(target){
        //计算出朝向
        var dx = target.x - this.node.x;
        var dy = target.y - this.node.y;
       
        var dir = cc.v2(dx,dy);
        //根据朝向计算出夹角弧度
        var angle = dir.signAngle(cc.v2(1,0));

        //将弧度转换为欧拉角
        var degree = angle / Math.PI * 180;
        //赋值给节点
        return degree;
    },

    update(dt){
        this.node.angle = -this.lookAtObj(this.targetPos)
        var angle = (this.node.angle) / 180 * Math.PI;
        //合成基于 X正方向的方向向量
        var dir = cc.v2(Math.cos(angle),Math.sin(angle));
        //单位化向量
        dir.normalizeSelf();
    
        this.node.x += 100*dir.x/20
        this.node.y += 100*dir.y/20

        this.checkMove()
    },

    checkMove(){
        var enemyNode = G_wuxingfenghuolu.gameObj.enemyNode;
        for (var m = 0; m < enemyNode.childrenCount;m++) {
            if(enemyNode.getChildren()[m].getComponent(enemyNode.getChildren()[m].name).getDie()==false){
                var mOm = this.node.position.sub(enemyNode.getChildren()[m].position)
                var tmpDis = Math.abs(mOm.mag());
                if(tmpDis<enemyNode.getChildren()[m].width/2){
                    enemyNode.getChildren()[m].getComponent(enemyNode.getChildren()[m].name).action_shui1(this.node,this.lv);
                    this.createJiansuArea()
                    this.node.destroy();
                    return;
                }
            }
        }
        var tmpDis1 = Math.abs(this.node.position.sub(this.targetPos).mag());
        if(tmpDis1<5){
            this.createJiansuArea()
            this.node.destroy();
        }
    },

    createJiansuArea(){
        var bullet_shui1_xiaoguo = cc.instantiate(cc.globalRes["prefab_bullet_shui1_xiaoguo"]);
        bullet_shui1_xiaoguo.parent = G_wuxingfenghuolu.gameObj.bulletsNode;
        bullet_shui1_xiaoguo.position = this.node.position;
        bullet_shui1_xiaoguo.getComponent("bullet_shui1_xiaoguo").setInfo(this.lv);
    },



});
